//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class View3D : IOfficeXmlSerializable, INotifyPropertyChanged
    {
        private int _depthPercent;
        private int _hPercent;
        private int _perspective = 30;
        private bool _rAngAx;
        private int _rotationX = defaultRotationX;
        private int _rotationY = defaultRotationY;
        private const int defaultRotationX = 20;
        private const int defaultRotationY = 15;
        public int RotationX
        {
            get { return _rotationX; }
            set { _rotationX = value; notifyPropertyChanged("RotationX"); }
        }

        public int HPercent
        {
            get { return _hPercent; }
            set { _hPercent = value; notifyPropertyChanged("HPercent"); }
        }

        public int RotationY
        {
            get { return _rotationY; }
            set { _rotationY = value; notifyPropertyChanged("RotationY"); }
        }

        public int DepthPercent
        {
            get { return _depthPercent; }
            set { _depthPercent = value; notifyPropertyChanged("DepthPercent"); }
        }

        public bool RightAngleAxes
        {
            get { return _rAngAx; }
            set { _rAngAx = value; notifyPropertyChanged("RightAngleAxes"); }
        }

        public int Perspective
        {
            get { return _perspective; }
            set { _perspective = value;notifyPropertyChanged("Perspective"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("view3D"))
            {
                RotationX = c.ReadOptionalElementIntegerAttribute("rotX", defaultRotationX);
                HPercent = c.ReadOptionalElementIntegerAttribute("hPercent", 100);
                RotationY = c.ReadOptionalElementIntegerAttribute("rotY", defaultRotationY);
                DepthPercent = c.ReadOptionalElementIntegerAttribute("depthPercent", 0);
                RightAngleAxes = c.ReadOptionalElementBooleanAttribute("rAngAx", false, true);
                Perspective = c.ReadOptionalElementIntegerAttribute("perspective", 30);
                c.WarnIf("crtExt");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("view3D"))
            {
                c.WriteOptionalElementIntegerAttribute("rotX", RotationX, defaultRotationX);
                c.WriteElementIntegerAttribute("hPercent", HPercent); //\\optional
                c.WriteOptionalElementIntegerAttribute("rotY", RotationY, defaultRotationY);
                c.WriteOptionalElementIntegerAttribute("depthPercent", DepthPercent, 0);
                if (RightAngleAxes)
                    c.WriteOptionalElementBooleanAttribute("rAngAx", RightAngleAxes);
                c.WriteElementIntegerAttribute("perspective", Perspective); //\\optional
                c.WarnIf("crtExt");
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}